package zozopo.warehouse.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import zozopo.comm.CNSpell;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class WarehouseOS extends OperatingService{
	
	
	
	//private static final Log logger = LogFactory.getLog(WarehouseOS.class);
	
	public WarehouseOS(Connection conn){
		super(conn);
	}
	
	public WarehouseOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insertWarehouse(WarehouseTB warehouse){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();

		insertMap.put(WarehouseTB.ADDRESS_WAREHOUSE, warehouse.getAddress_warehouse());
		//insertMap.put(WarehouseTB.DATE_WAREHOUSE, goods.getUnit_goods());
		insertMap.put(WarehouseTB.MANAGER_WAREHOUSE, warehouse.getManager_warehouse());
		insertMap.put(WarehouseTB.MOBILE_WAREHOUSE, warehouse.getMobile_warehouse());
		insertMap.put(WarehouseTB.NAME_WAREHOUSE, warehouse.getName_warehouse());
		insertMap.put(WarehouseTB.PHONE_WAREHOUSE, warehouse.getPhone_warehouse());
		insertMap.put(WarehouseTB.QQ_WAREHOUSE, warehouse.getQq_warehouse());
		
		insertMap.put(WarehouseTB.REMARK_WAREHOUSE, warehouse.getRemark_warehouse());
		insertMap.put(WarehouseTB.USERMAIL_WAREHOUSE, warehouse.getUseremail_warehouse());
		insertMap.put(WarehouseTB.USERNAME_WAREHOUSE, warehouse.getUsername_warehouse());
		
		//
	
		
		
		
		return sqlMachine.insertRow(WarehouseTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean upadteWarehouse(WarehouseTB warehouse){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(WarehouseTB.ADDRESS_WAREHOUSE, warehouse.getAddress_warehouse());
		//insertMap.put(WarehouseTB.DATE_WAREHOUSE, goods.getUnit_goods());
		updateMap.put(WarehouseTB.MANAGER_WAREHOUSE, warehouse.getManager_warehouse());
		updateMap.put(WarehouseTB.MOBILE_WAREHOUSE, warehouse.getMobile_warehouse());
		updateMap.put(WarehouseTB.NAME_WAREHOUSE, warehouse.getName_warehouse());
		updateMap.put(WarehouseTB.PHONE_WAREHOUSE, warehouse.getPhone_warehouse());
		updateMap.put(WarehouseTB.QQ_WAREHOUSE, warehouse.getQq_warehouse());
		
		updateMap.put(WarehouseTB.REMARK_WAREHOUSE, warehouse.getRemark_warehouse());
		updateMap.put(WarehouseTB.USERMAIL_WAREHOUSE, warehouse.getUseremail_warehouse());
		updateMap.put(WarehouseTB.USERNAME_WAREHOUSE, warehouse.getUsername_warehouse());
		
		
		
		
		return sqlMachine.updateRow(WarehouseTB.TABLE_NAME, updateMap, WarehouseTB.ID_WAREHOUSE, warehouse.getId_warehouse());
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<WarehouseTB> selectListBySql(String sql){
		List<WarehouseTB> warehouseList = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					warehouseList = new ArrayList<WarehouseTB>();
					while(resultSet.next()){
						WarehouseTB warehouse = new WarehouseTB();
						
						warehouse.setId_warehouse(resultSet.getString(WarehouseTB.ID_WAREHOUSE));
						warehouse.setAddress_warehouse(resultSet.getString(WarehouseTB.ADDRESS_WAREHOUSE));
						warehouse.setDate_warehouse(resultSet.getString(WarehouseTB.DATE_WAREHOUSE));
						warehouse.setManager_warehouse(resultSet.getString(WarehouseTB.MANAGER_WAREHOUSE));
						warehouse.setMobile_warehouse(resultSet.getString(WarehouseTB.MOBILE_WAREHOUSE));
						warehouse.setName_warehouse(resultSet.getString(WarehouseTB.NAME_WAREHOUSE));
						warehouse.setPhone_warehouse(resultSet.getString(WarehouseTB.PHONE_WAREHOUSE));
						warehouse.setQq_warehouse(resultSet.getString(WarehouseTB.QQ_WAREHOUSE));
						
						warehouse.setUseremail_warehouse(resultSet.getString(WarehouseTB.USERMAIL_WAREHOUSE));
						warehouse.setUsername_warehouse(resultSet.getString(WarehouseTB.USERNAME_WAREHOUSE));
						
						warehouse.setRemark_warehouse(resultSet.getString(WarehouseTB.REMARK_WAREHOUSE));
						
						warehouseList.add(warehouse);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return warehouseList;
	
	}
	public List<WarehouseTB> selectWarehouse(int beginPageNum,int pageSize){
		
		List<WarehouseTB> warehouseList = null;
		String sql = "SELECT * FROM " + WarehouseTB.TABLE_NAME  +" limit "+beginPageNum+","+pageSize; 
		warehouseList = selectListBySql(sql);
		
		return warehouseList;
	}
	
	public List<WarehouseTB> select(){
		
		List<WarehouseTB> warehouseList = null;
		String sql = "SELECT * FROM " + WarehouseTB.TABLE_NAME; 
		warehouseList = selectListBySql(sql);
		
		return warehouseList;
	}
	
	public WarehouseTB getWarehouseById(String id){
		String sql = "SELECT * FROM " + WarehouseTB.TABLE_NAME + " where " + WarehouseTB.ID_WAREHOUSE + "=" + id;
		WarehouseTB warehouse = new WarehouseTB();
		List<WarehouseTB> warehouseList = selectListBySql(sql);
		warehouse = warehouseList.get(0);
		return warehouse;
	}
	
	public WarehouseTB getWarehouseById(int id){
		String sql = "SELECT * FROM " + WarehouseTB.TABLE_NAME + " where " + WarehouseTB.ID_WAREHOUSE + "=" + id;
		WarehouseTB warehouse = new WarehouseTB();
		List<WarehouseTB> warehouseList = selectListBySql(sql);
		warehouse = warehouseList.get(0);
		return warehouse;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public int getMaxIdByEmail(String email){
		//String sql = "select * from "+ReleaseTB.TABLE_NAME+" order by  "+ReleaseTB.RELEASE_DATE+" desc limit "+beginPageNum+","+pageSize; 
		SqlMachine sqlMachine = new SqlMachine(conn);
		int maxId = sqlMachine.getMaxId(WarehouseTB.ID_WAREHOUSE, WarehouseTB.TABLE_NAME, WarehouseTB.USERMAIL_WAREHOUSE, email);
		return maxId;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNum(){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + WarehouseTB.TABLE_NAME ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<WarehouseTB> selectBySearchStr(String searchStr,int beginPageNum,int pageSize){
		String sql = "select * from "+WarehouseTB.TABLE_NAME+" WHERE  "
		+WarehouseTB.ID_WAREHOUSE+" LIKE '%"+searchStr+"%'"
		+" OR "+WarehouseTB.ADDRESS_WAREHOUSE+" LIKE '%"+searchStr+"%'"
		+" OR "+WarehouseTB.NAME_WAREHOUSE+" LIKE '%"+searchStr+"%'"
		+" OR "+WarehouseTB.MANAGER_WAREHOUSE+" LIKE '%"+searchStr+"%'"
		+" limit "+beginPageNum+","+pageSize; 
		//ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		return selectListBySql(sql);
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNumForSearchStr(String searchStr){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + GoodsTB.TABLE_NAME +" WHERE  "
		+WarehouseTB.ID_WAREHOUSE+" LIKE '%"+searchStr+"%'"
		+" OR "+WarehouseTB.ADDRESS_WAREHOUSE+" LIKE '%"+searchStr+"%'"
		+" OR "+WarehouseTB.NAME_WAREHOUSE+" LIKE '%"+searchStr+"%'"
		+" OR "+WarehouseTB.MANAGER_WAREHOUSE+" LIKE '%"+searchStr+"%'";
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	
	
}
